package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//key=&saleable=true&page=1&rows=5
@RestController
@RequestMapping("spec")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @RequestMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid") Long cid){

        List<SpecGroup> groups = specificationService.queryGroupsByCid(cid);
        if (CollectionUtils.isEmpty(groups)){
            return  ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groups);
    }

    @GetMapping("params")
    public  ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid", required = false)Long gid,
            @RequestParam(value = "cid", required = false)Long cid,
            @RequestParam(value = "generic", required = false)Boolean generic,
            @RequestParam(value = "searching", required = false)Boolean searching
    ){
        List<SpecParam> params = specificationService.queryParams(gid,cid,generic,searching);
        if (CollectionUtils.isEmpty(params)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(params);
    }

    @PostMapping("group")
    public void addGroup(@RequestBody SpecGroup group){
        specificationService.addGroup(group);
    }

    @PutMapping("group")
    public  void editGroup(@RequestBody SpecGroup specGroup){
        this.specificationService.editGroup(specGroup);
    }

    @PostMapping("param")
    public  void addParam(@RequestBody SpecParam param){
        this.specificationService.addParam(param);
    }


    @PutMapping("param")
    public  void editParam(@RequestBody SpecParam param){
        this.specificationService.editParam(param);
    }


    @DeleteMapping("param/{id}")
    public  void deleteParam(@PathVariable("id") Long id){
        this.specificationService.deleteParam(id);
    }

    @DeleteMapping("group/{id}")
    public void deleteGroups(@PathVariable("id") Long id){
        this.specificationService.deleteGroup(id);
    }
    /**
     *查询规格组，同时在规格组内的所有参数。
     * @Date  2020/1/1 11:50
     * @param cid
     * @return
     **/
   @GetMapping("group/param/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupWithParam(@PathVariable("cid") Long cid){
        List<SpecGroup> groups = this.specificationService.queryGroupWithParam(cid);

       if(CollectionUtils.isEmpty(groups)){
           return new ResponseEntity<>(HttpStatus.NOT_FOUND);
       }
       return ResponseEntity.ok(groups);
    }
}
